<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button type="primary" size="mini" plain icon="el-icon-plus" @click="handleAdd">新增</el-button>
      <el-button
        type="success"
        size="mini"
        plain
        icon="el-icon-edit"
        :disabled="buttonState.editDisabled"
        @click="toolBarHandleUpdate"
      >
        修改
      </el-button>
      <el-button
        type="danger"
        size="mini"
        plain
        icon="el-icon-delete"
        :disabled="buttonState.delDisabled"
        @click="toolBarHandleDelete"
      >
        删除
      </el-button>
      <right-toolbar @queryTable="refreshPage"></right-toolbar>
    </div>
    <el-table
      :data="tableData"
      show-summary
      :summary-method="getSummaries"
      style="width: 100%"
      ref="salaryTable"
      v-loading="loading"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="remark"
        label="备注"
        align="center"
        width="350"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column prop="salaryMicrometer" label="工资(元)" width="150">
        <template slot-scope="scope">
          <el-tag type="primary">￥{{ scope.row.salaryMicrometer }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="issueYear"
        label="年份"
        align="center"
        width="150"
      ></el-table-column>
      <el-table-column
        prop="issueMonth"
        label="月份"
        align="center"
        width="150"
      ></el-table-column>
      <el-table-column
        prop="issueDate"
        label="工资发放日期"
        align="center"
        width="150"
      ></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="handleUpdate(scope.row.salaryId)">
            修改
          </el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete" @click="handleDelete(scope.row.salaryId)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增或修改工资对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form :model="form" ref="form" :rules="rules" label-width="80px">
        <input type="hidden" v-model="form.salaryId" />
        <el-form-item label="工资" prop="salary">
          <el-input-number style="width: 100%" v-model="form.salary" placeholder="请输入工资" :min="1" :max="99999999.99"></el-input-number>
        </el-form-item>
        <el-form-item label="发放日期" prop="issueDate">
          <el-date-picker
            v-model="form.issueDate"
            value-format="yyyy-MM-dd"
            type="date"
            placeholder="请选择发放日期"
            :editable="false"
            class="el-date-picker-custom"
            :picker-options="pickerOptions"
            :clearable="false">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" placeholder="请输入备注，空则自动生成"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click.native="submitForm" :disabled="buttonState.submitDisabled">确 定</el-button>
        <el-button @click.native="cancelHandler">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: 'MySalary',
  data() {
    return {
      tableData: null,
      totalSalary: '',
      loading: true,
      buttonState: {
        editDisabled: true,
        delDisabled: true,
        submitDisabled: false
      },
      multipleSelection: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单验证规则
      rules: {
        salary: [
          { required: true, message: "请输入正确的工资，范围1-99999999.99", trigger: 'blur'}
        ],
        issueDate: [
          { required: true, message: "请选择发放日期", trigger: 'blur'}
        ]
      },
      pickerOptions: {
        disabledDate(time) {
          let currentYear = new Date().getFullYear();
          return time.getFullYear() !== currentYear 
        }
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 通过勾选复选框并点击工具栏删除按钮删除工资数据
    toolBarHandleDelete() {
      if (this.multipleSelection.length <= 0) {
        return;
      }
      this.handleDelete(this.multipleSelection);
    },
    // 删除
    handleDelete(salaryId) {
      this.$confirm('此操作将永久删除该条工资记录 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest(`/salary/my/${salaryId}`).then(() => {
          this.getData()
        })
      }).catch(() => {          
      });
    },
    // 通过勾选复选框并点击工具栏修改按钮打开修改对话框
    toolBarHandleUpdate() {
      if (this.multipleSelection.length <= 0 || this.multipleSelection.length > 1) {
        return;
      }
      
      let salaryId = this.multipleSelection[0];
      this.handleUpdate(salaryId);

    },
    // 打开修改对话框
    handleUpdate(salaryId) {
      this.title = "修改工资"
      this.getRequest(`/salary/my/${salaryId}`).then((resp) => {
        this.form = resp.data;
        this.open = true;
      })
    },
    // 新增/修改工资
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonState.submitDisabled = true;
          if (!this.form.salaryId) {
            // 新增
            this.postRequest("/salary/my/", this.form).then(() => {
              this.open = false;
              this.buttonState.submitDisabled = false;
              this.getData()
            }).catch(() => {
              this.buttonState.submitDisabled = false;
            })
          } else {
            // 修改
            this.putRequest('/salary/my/', this.form).then(() => {
              this.open = false;
              this.buttonState.submitDisabled = false;
              this.getData()
            }).catch(() => {
              this.buttonState.submitDisabled = false;
            })
          }
        }
      });
    },
    // 关闭新增对话框
    cancelHandler() {
      this.open = false;
      this.reset();
    },
    // 打开新增对话框
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增工资";
    },
    // 重置表单
    reset() {
      this.form = {
        issueDate: this.parseTime(new Date(), "{yyyy}-{mm}-{dd}")
      }
      this.resetForm("form");
    },
    handleSelectionChange(val) {
      this.multipleSelection = val.map(item => item.salaryId);
      if (this.multipleSelection.length <= 0) {
        this.buttonState.delDisabled = true
      } else {
        this.buttonState.delDisabled = false
      }

      if (this.multipleSelection.length == 1) {
        this.buttonState.editDisabled = false
      } else {
        this.buttonState.editDisabled = true
      }
    },
    refreshPage() {
      this.loading = true
      this.getData()
    },
    getData() {
      this.getRequest('/salary/my/').then((resp) => {
        this.tableData = resp.data.list.result
        this.totalSalary = resp.data.list.total
        this.loading = false
      })
    },
    getSummaries(param) {
      const { columns, data } = param
      const sums = []

      columns.forEach((column, index) => {
        if (index === 0) {
          return
        }

        if (index === 1) {
          sums[index] = (
            <span style="color: #515a6e; font-size: 14px; font-weight: bold">
              {new Date().getFullYear()}年工资总计
            </span>
          )
          return
        }

        if (column.property === 'salaryMicrometer') {
          sums[index] = <el-tag type="primary" style="font-weight: bold; font-size: 13px">￥{this.totalSalary} 元</el-tag>
        }
      })
      return sums
    },
  },
}
</script>

<style lang="scss" scoped>

</style>
